home *** CD-ROM | disk | FTP | other *** search
- Path: kbad.eglin.af.mil!rpi!not-for-mail
- From: David Carr <davidc@marine.simrad.no>
- Newsgroups: comp.lang.c++,comp.lang.c++.moderated,comp.lang.c.moderated
- Subject: Q: Rigorous coding using #if !defined(...) and #include
- Date: 31 Jan 1996 15:22:36 -0000
- Organization: EUnet Norway
- Sender: cppmods@netlab.cs.rpi.edu
- Approved: kanze@lts.sel.alcatel.de
- Message-ID: <4eo1fs$rr3@netlab.cs.rpi.edu>
- NNTP-Posting-Host: netlab.cs.rpi.edu
- X-Original-Date: Wed, 31 Jan 1996 15:57:28 +0100
-
- Q: Rigorous coding w #if !defined
-
- Hi!
-
- I have been used to following rigorous coding standards. This
- has translated
- into a clear separation between H and source files, and making
- thorough
- use of compiler directives. In header files, I have used the
- notation;
-
- #if !defined (__MYCLASS_H)
- #define (__MYCLASS_H)
-
- #include "Other.h"
-
- class MyClass
- {
- ...// Uses something in Other.h
- };
-
- #endif
-
- where Other.h is the header file(s) that define the classes,
- types, and/or
- operations used within the MyClass.h file. This approach also
- explicitly
- binds all the dependencies between code (i.e. so builds are
- correct and up
- to date).
-
- Okay, nuff of the preamble. The problem is this. I have
- another class;
-
- #if !defined (__OTHER_H)
- #define (__OTHER_H)
-
- #include "MyClass.h"
-
- class Other
- {
- ...// Uses something in MyClass.h
- };
-
- #endif
-
- and in my source for MyClass.cpp, MyClass.h is obviously
- included. Now,
- when I try to compile MyClass.cpp, MyClass.h is included and
- __MYCLASS_H
- becomes defined. Then, MyClass.h #includes Other.h, thus
- defining __OTHER_H.
- Now, Other.h #includes MyClass.h. As __MYCLASS_H has already
- been defined,
- the code in MyClass.h is not included. Returning to Other.h,
- this code
- references something in MyClass.h (i.e. its class) and a
- compiler error is
- generated because the symbol/identifier/whatever is unknown.
-
- So, upon looking at VC++ examples and the code automatically
- generated, it
- seems that VC++ solves this circular problem by not using
- #includes within
- H files (at least not normally). The dependency relationships
- are all
- determined within the CPP file. I DO NOT like this because then
- if I want to
- use MyClass elsewhere, I am responsible for tracing through and
- #including
- all the required dependencies. To use the objects in an H file,
- I just want
- to include the file without any concern for ordering of the H
- files or being
- responsible for creating its dependencies.
-
- Now that most people who aren't interested have fallen asleep:
-
- What is the preferred method for writing rigorous code and
- solving this
- problem?
- Any suggestions as to how to make H files a complete interface?
-
- Thanks in advance,
-
- David Carr
- - -Canuck displaced in Bergen, Norway-
- davidc@simrad.marine.no
-
- [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
- [ Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm ]
- [ Moderation policy: http://www.connobj.com/cpp/guide.htm ]
- [ Comments? mailto:c++-request@netlab.cs.rpi.edu ]
-